package com.gelsanalyzer.mvc.controller;

import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import com.gelsanalyzer.business.*;
import com.gelsanalyzer.core.*;
import com.gelsanalyzer.ui.swing.*;
import com.gelsanalyzer.ui.swing.command.*;

/**
 * This class is the "Controller" in our MVC framework. It has a dependent
 * knowledge of the View and the Model.
 * 
 * @author Jared Olhoft
 */
@Deprecated
public class CourseEntryHandler extends AbstractController {
    // This is the "View" in M*V*C...
    private CourseEntryFrame courseEntryFrame;
    private CourseEntryPanel courseEntryPanel;
    private CourseMgmtMenuBar courseMenuBar;
    private CourseEntryList courseEntryList;

    // The following are the "Model" in *M*VC...
    private UserSession userSession;
    private List<Department> departments;

    public CourseEntryHandler(UserSession userSession) {
        // TODO: modify this method once we get the MVC framework established
        super(null,null);
       
        this.userSession = userSession;
     
        courseEntryFrame = new CourseEntryFrame("Enter Courses", userSession
            .getUser().getFirstName(), this);
        courseEntryPanel = courseEntryFrame.getCourseEntryPanel();
        courseMenuBar = courseEntryFrame.getCourseMgmtMenuBar();
        courseEntryList = (CourseEntryList) courseEntryPanel.getCourseTable()
            .getModel();

        addActionListeners();
        ((AddCourseCommand)ActionFactory.getAddCourse()).setController(this);
        courseEntryFrame.pack();
        courseEntryFrame.setVisible(true);
        throw new RuntimeException("HEY");
    }

    public CourseEntryFrame getCourseEntryFrame() {
        return courseEntryFrame;
    }

    public CourseMgmtMenuBar getCourseMenuBar() {
        return courseMenuBar;
    }

    public CourseEntryPanel getCourseEntryPanel() {
        return courseEntryPanel;
    }

    public void actionPerformed(ActionEvent ae) {
        Command obj = (Command) ae.getSource();
        obj.Execute();
    }

    public List<Department> firePopulateDepartments() {
        DepartmentHome dh = new DepartmentHome();
        return dh.findAllDepartments();
    }

    public List<Course> firePopulateCourses(DepartmentComboCommand departmentComboCommand,
                                            Department department) {
        CourseHome ch = new CourseHome();
        return ch.findByDepartmentExample(department);
    }

    public void fireAddCourse() {
        Course course = (Course) courseEntryPanel.getCourseNums()
            .getSelectedItem();
        Department department = (Department) courseEntryPanel.getDepartments()
            .getSelectedItem();
        if ( course == null ) {
            JOptionPane.showMessageDialog(courseEntryFrame, "Cannot add null course",
                "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }else if ( department == null ) {
            fireNullDepartmentSelected();
            return;
        }

        CourseEntryItem CEI = new CourseEntryItem(course, department);

        if ( courseEntryList.getIndexof(CEI) >= 0 ) {
            JOptionPane.showMessageDialog(courseEntryFrame, "Course already appears in"
                    + " list, please enter another course.", "Error",
                JOptionPane.ERROR_MESSAGE);
        }else {
            courseEntryList.addCourseEntryItem(CEI);            
        }
    }

    private void addActionListeners() {
        courseEntryPanel.getAddCourseButton().addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    courseEntryFrame.getCourseMgmtMenuBar()
                        .getEditAddCourseMenuItem().doClick();
                }
            });
    }

    public void fireNullDepartmentSelected() {
        courseEntryPanel.getCourseNums().removeAllItems();
        courseEntryPanel.getCourseNums().setEnabled(false);
        courseEntryPanel.getAddCourseButton().setEnabled(false);
        courseMenuBar.getEditAddCourseMenuItem().setEnabled(false);
    }

//    @Override
//    public void update(Observable observable, Object object) {
//        System.out.println("in the update method...");
//        System.out.println("observable=" + observable);
//        System.out.println("object=" + object);
//    }
//
//    @Override
//    public void updateModels(Command command) {
//    }
//
//    @Override
//    public void updateModels(ActionEvent command) {
//    }

//    @Override
//    public void applicationEventFired(ActionEvent actionEvent) {
//        super.applicationEventFired(actionEvent);
//        this.fireAddCourse();
//    }
}
